home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevcfax.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  6.5 KB  |  236 lines

  1. /* Copyright (C) 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdevcfax.c,v 1.3 2000/09/19 19:00:11 lpd Exp $ */
  20. /* SFF format writer for CAPI fax devices */
  21. #include "gdevprn.h"
  22. #include "strimpl.h"
  23. #include "scfx.h"
  24. #include "gdevfax.h"
  25.  
  26. /* The device descriptor */
  27. private dev_proc_print_page(cfax_print_page);
  28. private dev_proc_close_device(cfax_prn_close);
  29.  
  30. /* Define procedures for cfax. For sff multipage documents  */
  31. /* a special close procedure is required because sff needs  */
  32. /* an additional "end of document" signature after the last */ 
  33. /* "end page" signature */
  34. private const gx_device_procs gdev_cfax_std_procs =
  35.     prn_params_procs(gdev_prn_open, gdev_prn_output_page, cfax_prn_close,
  36.              gdev_fax_get_params, gdev_fax_put_params);
  37.  
  38. const gx_device_fax gs_cfax_device = {
  39.     FAX_DEVICE_BODY(gx_device_fax, gdev_cfax_std_procs, "cfax", cfax_print_page)
  40. };
  41.  
  42. /* ---------------- SFF output ----------------- */
  43.  
  44. private void
  45. cfax_byte(uint c, FILE * file)
  46. {
  47.     fputc(c & 0xff, file);
  48. }
  49.  
  50. private void
  51. cfax_word(ushort c, FILE * file)
  52. {
  53.     cfax_byte(c & 0xff, file);
  54.     cfax_byte(c >> 8, file);
  55. }
  56.  
  57. private void
  58. cfax_dword(ulong c, FILE * file)
  59. {
  60.     cfax_byte(c & 0xff, file);
  61.     cfax_byte(c >> 8, file);
  62.     cfax_byte(c >> 16, file);
  63.     cfax_byte(c >> 24, file);
  64. }
  65.  
  66. private void
  67. cfax_doc_hdr(FILE * file)
  68. {
  69.     cfax_byte('S', file);
  70.     cfax_byte('f', file);
  71.     cfax_byte('f', file);
  72.     cfax_byte('f', file);
  73.     cfax_byte(1, file);
  74.     cfax_byte(0, file);
  75.     cfax_word(0, file);
  76.     cfax_word(0, file);
  77.     cfax_word(20, file);
  78.     cfax_dword(0, file);
  79.     cfax_dword(0, file);
  80. }
  81.  
  82. private void
  83. cfax_page_hdr(gx_device_printer * pdev, FILE * file)
  84. {
  85.     cfax_byte(254, file);
  86.     cfax_byte(16, file);
  87.     cfax_byte((pdev->y_pixels_per_inch < 100 ? 0 : 1), file);
  88.     cfax_byte(0, file);
  89.     cfax_byte(0, file);
  90.     cfax_byte(0, file);
  91.     cfax_word(pdev->width, file);
  92.     cfax_word(pdev->height, file);
  93.     cfax_dword(0, file);
  94.     cfax_dword(0, file);
  95. }
  96.  
  97. private void
  98. cfax_doc_end(FILE * file)
  99. {
  100.     cfax_byte(254, file);
  101.     cfax_byte(0, file);
  102. }
  103.  
  104. /* Send the page to the printer. */
  105. private int
  106. cfax_stream_print_page_width(gx_device_printer * pdev, FILE * prn_stream,
  107.                  const stream_template * temp, stream_state * ss, 
  108.                             int width)
  109. {
  110.     gs_memory_t *mem = pdev->memory;
  111.     int code = 0;
  112.     stream_cursor_read r;
  113.     stream_cursor_write w;
  114.     int in_size = gdev_prn_raster((gx_device *) pdev);
  115.     /*
  116.      * Because of the width adjustment for fax systems, width may
  117.      * be different from (either greater than or less than) pdev->width.
  118.      * Allocate a large enough buffer to account for this.
  119.      */
  120.     int col_size = (width * pdev->color_info.depth + 7) >> 3;
  121.     int max_size = max(in_size, col_size);
  122.     int lnum, nbytes, i;
  123.     byte *in;
  124.     byte *out;
  125.     /* If the file is 'nul', don't even do the writes. */
  126.     bool nul = !strcmp(pdev->fname, "nul");
  127.  
  128.     /* Initialize the common part of the encoder state. */
  129.     ss->template = temp;
  130.     ss->memory = mem;
  131.  
  132.     /* Allocate the buffers. */
  133.     in = gs_alloc_bytes(mem, temp->min_in_size + max_size + 1, 
  134.         "cfax_stream_print_page(in)");
  135.  
  136. #define OUT_SIZE 1000
  137.     out = gs_alloc_bytes(mem, OUT_SIZE, "cfax_stream_print_page(out)");
  138.     if (in == 0 || out == 0) {
  139.     code = gs_note_error(gs_error_VMerror);
  140.     goto done;
  141.     }
  142.  
  143.     /* Process the image */ 
  144.     for (lnum = 0; lnum < pdev->height; lnum++) {
  145.     /* Initialize read and write pointer each time, because they're getting modified */
  146.     r.ptr = in - 1;
  147.         r.limit = in + col_size;
  148.         w.ptr = out - 1;
  149.         w.limit = w.ptr + OUT_SIZE;
  150.     /* Decoder must encode line for line, so init it for each line */
  151.     code = (*temp->init) (ss);
  152.     if (code < 0)
  153.         return_error(gs_error_limitcheck);
  154.     /* Now, get the bits and encode them */
  155.     gdev_prn_copy_scan_lines(pdev, lnum, in, in_size);
  156.     if (col_size > in_size) {
  157.         memset(in + in_size , 0, col_size - in_size);
  158.         }
  159.     code = (*temp->process) (ss, &r, &w, 1 /* always last line */);
  160.     nbytes = w.ptr - out + 1;
  161.     if (!nul) {
  162.         if (nbytes > 0) {
  163.         if (nbytes < 217) {
  164.             cfax_byte(nbytes, prn_stream);
  165.             for (i = 0; i < nbytes; i++)
  166.               cfax_byte(out[i], prn_stream);
  167.         } else {
  168.             cfax_byte(0, prn_stream);
  169.             cfax_word(nbytes, prn_stream);
  170.             for (i = 0; i < nbytes; i++)
  171.               cfax_byte(out[i], prn_stream);
  172.         }
  173.         } else {
  174.         cfax_byte(218, prn_stream);
  175.         }
  176.     } 
  177.     if (temp->release != 0)
  178.         (*temp->release) (ss);
  179.     }
  180. #undef OUT_SIZE
  181.  
  182.   done:
  183.     gs_free_object(mem, out, "cfax_stream_print_page(out)");
  184.     gs_free_object(mem, in, "cfax_stream_print_page(in)");
  185.     return code;
  186. }
  187.  
  188. /* Begin a capi fax page. */
  189. private int
  190. cfax_begin_page(gx_device_printer * pdev, FILE * fp, int width)
  191. {
  192.     /* Patch the width to reflect fax page width adjustment. */
  193.     int save_width = pdev->width;
  194.  
  195.     pdev->width = width;
  196.     if (gdev_prn_file_is_new(pdev)) {
  197.     cfax_doc_hdr(fp);
  198.     }
  199.     cfax_page_hdr(pdev, fp);
  200.  
  201.     pdev->width = save_width;
  202.     return 0;
  203. }
  204.  
  205. /* Print an capi fax (sff-encoded) page. */
  206. private int
  207. cfax_print_page(gx_device_printer * pdev, FILE * prn_stream)
  208. {
  209.     stream_CFE_state state;
  210.     int code;
  211.  
  212.     gdev_fax_init_fax_state(&state, (gx_device_fax *)pdev);
  213.     state.EndOfLine = false;
  214.     state.EndOfBlock = false;
  215.     state.EncodedByteAlign = true;
  216.     state.FirstBitLowOrder = true;
  217.     state.K = 0;
  218.  
  219.     cfax_begin_page(pdev, prn_stream, state.Columns);
  220.     code = cfax_stream_print_page_width(pdev, prn_stream, 
  221.       &s_CFE_template, (stream_state *) &state, state.Columns);
  222.     return code;
  223. }
  224.  
  225. /* Close an capi fax (sff-encoded) document. */
  226. private int
  227. cfax_prn_close(gx_device * pdev)
  228. {
  229.     gx_device_printer * const ppdev = (gx_device_printer *)pdev;
  230.  
  231.     if (ppdev->file != NULL) {
  232.       cfax_doc_end(ppdev->file);
  233.     }
  234.     return gdev_prn_close(pdev);
  235. }
  236.